package com.houdoudou.bean;

import javax.servlet.http.HttpServletRequest;

public class Programmer {
   String telnum="";
   String secret="";
   int type=2;
   

public Programmer() {
	
}

public Programmer(String telnum, String secret, int type) {
	super();
	this.telnum = telnum;
	this.secret = secret;
	this.type = type;
}


public int getType() {
	return type;
}

public void setType(int type) {
	this.type = type;
}


public String getTelnum() {
	return telnum;
}

public void setTelnum(String telnum) {
	this.telnum = telnum;
}

public String getSecret() {
	return secret;
}

public void setSecret(String secret) {
	this.secret = secret;
}
   
public static  Programmer merge(Programmer programmerExist, Programmer programmerGiven){
	

		if(!programmerGiven.getSecret().isEmpty()){
			programmerExist.setSecret(programmerGiven.getSecret());
		}
	
		if(programmerGiven.getType()!=2){
			programmerExist.setType(programmerGiven.getType());
		}

	
	return programmerExist;
}	

public static Programmer beanFromRequest(HttpServletRequest req)  throws Exception {
	
	Programmer programmer= new Programmer();
	if(req.getParameter("secret")!=null){	
			programmer.setSecret(req.getParameter("secret"));
           }
	if(req.getParameter("telnum")!=null){	
			programmer.setTelnum(req.getParameter("telnum"));
           }
	if(req.getParameter("type")!=null){		
		if(!req.getParameter("type").isEmpty()){
			int type=Integer.parseInt(req.getParameter("type"));
			programmer.setType(type);}
           }	
	System.out.println("ProgrammerBean"+"secret="+programmer.getSecret()+"   telnum="+programmer.getTelnum()+"   type="+programmer.getType());
	
	return programmer;
}

public static void  checkValid(Programmer programmer) throws Exception{
	if(programmer.getTelnum().length()!=11){ throw new BeanParseException("程序员电话位数不对"); }
	if(programmer.getSecret().length()==0){ throw new BeanParseException("未设置密码"); }
	if((programmer.getType()!=1)&&(programmer.getType()!=2)){ throw new BeanParseException("未设置程序员类型"); }
	
  }

public static Programmer encrypt(Programmer programmer){
	 
	 try {  
		          System.out.println("加密 原文:" + programmer.getSecret());  
		            byte[] salt = PasswordUtil.getSalt();  
		           String ciphertext = PasswordUtil.encrypt(programmer.getSecret());  
		            System.out.println("加密 密文:" + ciphertext);  
		            programmer.setSecret(ciphertext);
		            String plaintext = PasswordUtil.decrypt(ciphertext);  
		             System.out.println("明文:" + plaintext);  
		         } catch (Exception e) {  
		            e.printStackTrace();  
		       }  

	
	return programmer;
}

public static Programmer decrypt(Programmer programmer) throws Exception{

		          System.out.println("解密 密文:" + programmer.getSecret()); 	           
		            String plaintext = PasswordUtil.decrypt(programmer.getSecret());  
		            programmer.setSecret(plaintext);
		             System.out.println("解密 明文:" + plaintext);  
	
	return programmer;
}

}
